/********************************* (C) COPYRIGHT 2019 曼珠沙华 ****************************************************
 * File Name          : flash.h
 * Author             : @曼珠沙华
 * Date               : 2019/8/27
 * Description        : flash file, flash函数头文件编写
 *
 * Define ------------------------------------------------------------------*/
#ifndef __FLASH_H
#define __FLASH_H

#include "main.h"

#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t


#define FLASH_CS PBout(12)

// 指令表
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04

#define W25X_ReadStatusReg 0x05
#define W25X_WriteStatusReg 0x01

#define W25X_ReadData 0x03
#define PageDataRead 0x13

#define W25X_PageProgram 0x02
#define W25X_ProgramExecute 0x10

#define W25X_BlockErase 0xD8

#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB

#define W25X_JedecDeviceID 0x9F

#define W25X_Reset 0xFF

// 寄存器地址
#define Protection_Register 0xA1
#define Configuration_Register 0xB1
#define Status_Register 0xC1

// flash size
#define Page_size 0x800   // 2048 bytes per page
#define Spare_size 0x40   // 64 bytes per page spare area
#define Block_size 0x40   // 64 pages	per block
#define Block_count 0x400 // 1024 blocks per 1G NAND

// volatile u16 Page_Add = 0;    //PA地址定义
///***********************
//         bit 0 - 5    页地址（0 - 63）
//				 bit 6 - 15   块地址（0 - 1023）
//************************/

void flash_init(void);
u8   W25_ReadSR(u8 sr);
void W25_WriteSR(u8 sr, u8 data);
void W25_Write_Enable(void);
void W25_Write_Disable(void);
u16  W25_ReadID(void);
void W25_PageDataRead(u16 BlockAddr, u8 PageAddr);
void W25_Read(u8* pBuffer, u16 BytesAddr, u16 BlockAddr, u8 PageAddr, u16 NumByteToRead);
void W25_WriteBuff(u16 CA_add, u8* pBuffer, u16 NumByteToWrite);
void W25_WriteData(u8* pBuffer, u16 BytesAddr, u16 BlockAddr, u8 PageAddr, u16 NumByteToWrite);
void W25_Write_NoCheck(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
void W25_Write(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite);
void W25_Erase_Sector(u16 Dst_Addr);
void W25_Wait_Busy(void);
void W25_PowerDown(void);
void W25_WAKEUP(void);
void W25_Reset(void);

#endif

/************************************* (C) COPYRIGHT 2019 @曼珠沙华 **********************************************/
/*********************************************   END OF FILE   ***************************************************/
